<?php
class Admin_OrderController extends Apps_Controller_Action_Admin {
    public function indexAction() {
		$this->_helper->redirector('list', null, null);
    }

    public function listAction() {
		$this->actResolve('act_delete',
						  'delete', null, null, array('id' => $this->_getParam('item_checker')),
					 	  true);

		$tbOrder = new M_DbTable_Order();
		$grid 	= new Apps_Plugin_Grid('tour');

		$grid->setData($tbOrder->getList())
			 ->setTableCaption('List Order')
			 ->setTableColumnFormat(array(
			 		array('title'		=> 'STT',
			 			  'dataField'	=> '_rank_',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'ID',
			 			  'dataField'	=> 'id',
			 			  'width'		=> '1%',
			 			  'class'		=> 'center'),
			 		array('title'		=> 'Payment Type',
			 			  'html'		=> array('<a href="/admin/payment-type/index">%s</a>',
			 			  				   array('payment_type'))),
			 		array('title'		=> 'Booking Type',
			 			  'html'		=> array('<a href="/admin/order-status/index">%s</a>',
			 			  				   array('order_status'))),
			 		array('title'		=> 'Customer',
			 			  'width'		=> '10%',
			 			  'html'		=> array('<a href="/admin/customer/index">%s</a>',
			 			  				   array('customer_name'))),
			 		array('title'		=> 'Email',
			 			  'html'        => array('<a href="mailto:%s" target="_blank">%s</a>',
			 			  				  array('email', 'email'))),
			 		array('title'		=> 'Phone',
			 			  'dataField'	=> 'phone'),
			 		array('title'		=> 'Address',
			 			  'dataField'	=> 'address'),
			 		array('title'		=> 'Country',
			 			  'dataField'	=> 'country_residence'),
			 		array('title'		=> 'Category',
			 			  'html'		=> function($rs) {
			 			  						$labelOrderReference = new Apps_View_Helper_LabelOrderReferenceType();
			 			  						return $labelOrderReference->labelOrderReferenceType($rs['reference_type']);
											}),
			 		array('title'		=> 'Tour/ Cruise',
			 			  'html'		=> function($rs) {
												return	($rs['tour_id']) ?
													'<a href="/tour/detail?id='. $rs['tour_id'] .'">'. $rs['tour_title'] .'</a>' :
													'<a href="/cruise/detail?id='. $rs['cruise_id'] .'">'. $rs['cruise_name'] .'</a>';			 			  					}),
			 		array('title'       => 'Delete',
			 			  'html'        => array('<a href="/admin/order/delete?id=%s"><i class="icon-remove must-confirm" data-must-confirm-msg="Xóa TT khác hàng" title="Xóa"></i></a>',
			 			  				   array('id')),
			 			  'width'       => '2%')
			 		));

		$this->view->grid = $grid;
    }

    public function deleteAction() {
    	$this->disableLayout(true, true);

    	$rq = $this->_request;
    	$orderIds = $rq->getParam('id');
    	$tbOrder = new M_DbTable_Order();

    	if(is_numeric($orderIds)) {
    		$this->_flashMessenger->addMessage(
    				$tbOrder->safelyDelete(array('id = ?' => $orderIds)) ?
    				self::$msgDeleteSuccess :
    				self::$msgDeleteError
    		);
    		$this->goToActionIndex();
    	} elseif (is_array($orderIds)) {
    		foreach ($orderIds as $orderId) {
    			$tbOrder->safelyDelete(array('id = ?' => $orderId));
    		}
    		$this->_flashMessenger->addMessage(self::$msgDeleteSuccess);
    		$this->goToActionIndex();
    	} else {
    		$this->_flashMessenger->addMessage(self::$msgNoRecordFound);
    		$this->goToActionIndex();
    	}
    }
}